package com.zerui.manager.deviceTouch.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zerui.manager.common.mapper.EasyBaseMapper;
import com.zerui.manager.deviceTouch.entity.DeviceTouchDO;
import com.zerui.manager.web.cmd.DeviceTouchQryCmd;
import com.zerui.manager.web.vo.DeviceTouchPageVO;
import com.zerui.manager.web.vo.feign.DeviceInfoFeignVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 设备Mapper
 *
 * @author pyzhang
 * @since 2024-04-11
 */
@Mapper
public interface DeviceTouchMapper extends EasyBaseMapper<DeviceTouchDO> {

    @Select({"<script>",
            "SELECT tdt.id,tdt.device_id,tdt.device_code,tdt.device_name,tdt.device_manufacturer,tdt.device_ip,tdt.type_code,tdt.type_name"
                    + ",tdt.product_code,tdt.product_name,tdt.node_type,tdt.node_type_name,tdt.build_no,tdt.level_no,tdt.room_no,tdt.position,tdt.is_online"
                    + ",(select GROUP_CONCAT(id) from tb_device_group where json_contains(device_ids, tdt.device_id)) as org_id"
                    + ",(select GROUP_CONCAT(org_name) from tb_device_group where json_contains(device_ids, tdt.device_id)) as group_name"
                    + ",ifnull(tdt.update_time,tdt.create_time) as create_time"
                    + ",td.access,td.enable_status,td.curfew_status,td.timed_task_status,td.start_time,td.end_time"
                    + " FROM tb_device_touch tdt "
                    + " LEFT JOIN tb_device td on tdt.device_id = td.device_id"
                    + " WHERE tdt.del_flag = 0",
            "<if test=\"param.productCode != null and param.productCode!=\'\'\">",
            "   and tdt.product_code = #{param.productCode} ",
            "</if>",
            "<if test=\"param.deviceCode != null and param.deviceCode!=\'\'\">",
            "   and tdt.device_code = #{param.deviceCode}",
            "</if>",
            "<if test=\"param.buildNo != null and param.buildNo!=\'\'\">",
            "   and tdt.build_no = #{param.buildNo} ",
            "</if>",
            "<if test=\"param.levelNo != null and param.levelNo!=\'\'\">",
            "   and tdt.level_no = #{param.levelNo} ",
            "</if>",
            "<if test=\"param.roomNo != null and param.roomNo!=\'\'\">",
            "   and tdt.room_no = #{param.roomNo} ",
            "</if>",
            "<if test=\"param.deviceIds != null and param.deviceIds.size>0\">",
            "   and tdt.device_id in <foreach collection='param.deviceIds' item='id' open='(' separator=',' close=')'>",
            "${id}</foreach>",
            "</if>",
            "<if test=\"param.deviceName != null and param.deviceName!=\'\'\">",
            "   and tdt.device_name like %${param.deviceName}%",
            "</if>",
            "<if test=\"param.position != null and param.position!=\'\'\">",
            "   and tdt.position like %${param.position}%",
            "</if>",
//		    "<if test=\"param.typeCode != null and param.typeCode!=\'\'\">",
//		    "   and tdt.type_code = #{param.typeCode}",
//		    "</if>",
            "</script>"})
    Page<DeviceTouchPageVO> selectPage2(Page<DeviceTouchPageVO> page, @Param("param") DeviceTouchQryCmd deviceTouchQryCmd);

    @Select({"<script>",
            "SELECT tdt.device_id,tdt.device_code,tdt.device_name,tdt.product_code as type,tdt.product_name as type_name"
                    + ",tdt.position,tdt.is_online as status,(CASE WHEN td.enable_status=0 THEN 1 ELSE 0 END) as enable"
                    + " FROM tb_device_touch tdt "
                    + " LEFT JOIN tb_device td on tdt.device_id = td.device_id"
                    + " WHERE tdt.del_flag = 0",
            "<if test=\"param.productCode != null and param.productCode!=\'\'\">",
            "   and tdt.product_code = #{param.productCode} ",
            "</if>",
            "<if test=\"param.deviceCode != null and param.deviceCode!=\'\'\">",
            "   and tdt.device_code = #{param.deviceCode}",
            "</if>",
            "<if test=\"param.buildNo != null and param.buildNo!=\'\'\">",
            "   and tdt.build_no = #{param.buildNo} ",
            "</if>",
            "<if test=\"param.levelNo != null and param.levelNo!=\'\'\">",
            "   and tdt.level_no = #{param.levelNo} ",
            "</if>",
            "<if test=\"param.roomNo != null and param.roomNo!=\'\'\">",
            "   and tdt.room_no = #{param.roomNo} ",
            "</if>",
            "<if test=\"param.deviceIds != null and param.deviceIds.size>0\">",
            "   and tdt.device_id in <foreach collection='param.deviceIds' item='id' open='(' separator=',' close=')'>",
            "${id}</foreach>",
            "</if>",
            "<if test=\"param.deviceName != null and param.deviceName!=\'\'\">",
            "   and tdt.device_name like %${param.deviceName}%",
            "</if>",
            "<if test=\"param.position != null and param.position!=\'\'\">",
            "   and tdt.position like %${param.position}%",
            "</if>",
            "</script>"})
    List<DeviceInfoFeignVO> selectDeviceList(@Param("param") DeviceTouchQryCmd qryCmd);

    @Select({"<script>",
            "SELECT tdt.id,tdt.device_id,tdt.device_code,tdt.device_name,tdt.device_manufacturer,tdt.device_ip,tdt.type_code,tdt.type_name"
                    + ",tdt.product_code,tdt.product_name,tdt.node_type,tdt.node_type_name,tdt.build_no,tdt.level_no,tdt.room_no,tdt.position,tdt.is_online"
                    + ",(select GROUP_CONCAT(id) from tb_device_group where json_contains(device_ids, tdt.device_id)) as org_id"
                    + ",(select GROUP_CONCAT(org_name) from tb_device_group where json_contains(device_ids, tdt.device_id)) as group_name"
                    + ",ifnull(tdt.update_time,tdt.create_time) as create_time"
                    + ",td.access,td.enable_status,td.curfew_status,td.timed_task_status,td.start_time,td.end_time"
                    + " FROM tb_device_touch tdt "
                    + " LEFT JOIN tb_device td on tdt.device_id = td.device_id"
                    + " WHERE tdt.del_flag = 0",
            "<if test=\"deviceCode != null and deviceCode!=\'\'\">",
            "   and tdt.device_code = #{deviceCode}",
            "</if>",
            "</script>"})
    List<DeviceTouchPageVO> selectDeviceByCode(@Param("deviceCode") String deviceCode);
}